package com.example.service.impl;

import com.example.dao.BlogDao;
import com.example.dao.impl.BlogDaoImpl;
import com.example.model.Blog;
import com.example.service.BlogService;

import java.util.Date;
import java.util.List;

/**
 * 博客服务实现类
 */
public class BlogServiceImpl implements BlogService {
    private BlogDao blogDao = new BlogDaoImpl();
    
    @Override
    public int createBlog(Blog blog) {
        // 设置创建时间和更新时间
        Date now = new Date();
        blog.setCreateTime(now);
        blog.setUpdateTime(now);
        
        // 如果阅读量为空，设置为0
        if (blog.getViewCount() <= 0) {
            blog.setViewCount(0);
        }
        
        return blogDao.add(blog);
    }

    @Override
    public boolean deleteBlog(int id) {
        int result = blogDao.delete(id);
        return result > 0;
    }

    @Override
    public boolean updateBlog(Blog blog) {
        // 设置更新时间
        blog.setUpdateTime(new Date());
        int result = blogDao.update(blog);
        return result > 0;
    }

    @Override
    public Blog getBlogById(int id) {
        return blogDao.findById(id);
    }

    @Override
    public List<Blog> getAllBlogs() {
        return blogDao.findAll();
    }

    @Override
    public List<Blog> getBlogsByUserId(int userId) {
        return blogDao.findByUserId(userId);
    }

    @Override
    public List<Blog> getBlogsByCategory(String category) {
        return blogDao.findByCategory(category);
    }

    @Override
    public List<Blog> searchBlogs(String keyword) {
        return blogDao.search(keyword);
    }

    @Override
    public List<Blog> getHotBlogs(int limit) {
        return blogDao.findHotBlogs(limit);
    }

    @Override
    public boolean incrementViewCount(int id) {
        return blogDao.incrementViewCount(id);
    }
} 